#include "qbuttonmanager.h"

QButtonManager::QButtonManager(QListWidget* error,QVBoxLayout* layout,QObject* parent)
:QObject(parent),m_layout(layout),m_errorDisplay(error)
{
  m_icon[ERR_PLP_MISSING_ID]               = IHM_ICON_5000;
  m_icon[ERR_REF_MANU_MISSING_ID]           = IHM_ICON_5001;
  m_icon[ERR_MANU_MISSING_ID]             = IHM_ICON_5002;
  m_icon[ERR_FOOTPRINT_MISSING_ID]           = IHM_ICON_5003;
  m_icon[ERR_PLP_NOT_IN_BDD_ID]             = IHM_ICON_5004;
  m_icon[ERR_PLP_NOT_IN_BDD_BUT_EXPORT_ID]       = IHM_ICON_5005;
  m_icon[ERR_SAME_PLP_BUT_NOT_SAME_FOOTPRINT_ID]     = IHM_ICON_5006;
  m_icon[ERR_SAME_PLP_BUT_NOT_SAME_REF_ID]       = IHM_ICON_5007;
  m_icon[ERR_SAME_VALUE_BUT_NOT_SAME_FOOTPRINT_ID]   = IHM_ICON_5008;
  m_icon[ERR_SAME_VALUE_BUT_NOT_SAME_PLP_ID]       = IHM_ICON_5009;
  m_icon[ERR_SAME_REF_MANU_BUT_NOT_SAME_PLP_ID]     = IHM_ICON_5010;
  m_icon[ERR_SAME_REF_MANU_BUT_NOT_SAME_FOOTPRINT_ID] = IHM_ICON_5011;
  m_icon[ERR_NOT_SAME_VALUE_IN_PLP_AND_VALUE_ID] = IHM_ICON_5012;
  
  m_text[ERR_PLP_MISSING_ID]               = IHM_TEXT_5000;
  m_text[ERR_REF_MANU_MISSING_ID]           = IHM_TEXT_5001;
  m_text[ERR_MANU_MISSING_ID]             = IHM_TEXT_5002;
  m_text[ERR_FOOTPRINT_MISSING_ID]           = IHM_TEXT_5003;
  m_text[ERR_PLP_NOT_IN_BDD_ID]             = IHM_TEXT_5004;
  m_text[ERR_PLP_NOT_IN_BDD_BUT_EXPORT_ID]       = IHM_TEXT_5005;
  m_text[ERR_SAME_PLP_BUT_NOT_SAME_FOOTPRINT_ID]     = IHM_TEXT_5006;
  m_text[ERR_SAME_PLP_BUT_NOT_SAME_REF_ID]       = IHM_TEXT_5007;
  m_text[ERR_SAME_VALUE_BUT_NOT_SAME_FOOTPRINT_ID]   = IHM_TEXT_5008;
  m_text[ERR_SAME_VALUE_BUT_NOT_SAME_PLP_ID]       = IHM_TEXT_5009;
  m_text[ERR_SAME_REF_MANU_BUT_NOT_SAME_PLP_ID]     = IHM_TEXT_5010;
  m_text[ERR_SAME_REF_MANU_BUT_NOT_SAME_FOOTPRINT_ID] = IHM_TEXT_5011;
  m_text[ERR_NOT_SAME_VALUE_IN_PLP_AND_VALUE_ID] = IHM_TEXT_5012;

}

QButtonManager::~QButtonManager()
{
}


void QButtonManager::refresh(const QList<QListLine*>& errors)
{
	clearLayout();
	m_errors.clear();
	m_buttons.clear();
	foreach(QListLine* item, errors)
		m_errors[item->id()] << item;


	QHash<QString,QList<QListLine*> >::iterator it;
	for(it = m_errors.begin(); it != m_errors.end() ; it++)
	{
		QErrorButton* errorButton = new QErrorButton(it.key(),iconFromID(it.key()),textFromID(it.key()));		
		m_buttons[it.key()] = errorButton;
		connect(errorButton,SIGNAL(selected(const QString&)),this,SLOT(buttonChanged(const QString&)));
		m_layout->addWidget(errorButton);
	}
}

void QButtonManager::clearLayout()
{
  while(m_layout->count())
  {
    QWidget* widget = m_layout->itemAt(0)->widget();
    m_layout->removeWidget(widget);
    delete widget;
  }
}

QString QButtonManager::iconFromID(const QString& id)
{
  if(m_icon.contains(id))
    return m_icon[id];
  else
    return QString();
}


QString QButtonManager::textFromID(const QString& id)
{
  if(m_text.contains(id))
    return m_text[id];
  else
    return QString();
}


void QButtonManager::buttonChanged(const QString& id)
{
	if(m_buttons.contains(id))
	{
		foreach(QErrorButton* button, m_buttons)
		{
			if(button)
			{
				if(button->id() != id)
					button->deselect();
			}
		}

		while(m_errorDisplay->count())
			m_errorDisplay->takeItem(0);

		QList<QListLine*> list = m_errors[id];

		foreach(QListLine* line,list)
			m_errorDisplay->addItem((QListWidgetItem*) line);


		if(m_errorDisplay->count())
			m_errorDisplay->setCurrentRow(0);
	}
}

QList<quint32> QButtonManager::actualLines() const
{
  if(m_errorDisplay->currentItem())
  {
    QListLine* line = (QListLine*) m_errorDisplay->currentItem();
    return line->fileLines();
  }
  return QList<quint32>();
}

QString QButtonManager::currrentId() const
{
	foreach(QErrorButton* button, m_buttons)
	{
		if(button)
		{
			if(button->checked())
				return button->id();
		}
	}
	return QString();
}